package ssm.blog.controller.admin;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import ssm.blog.entity.PageBean;
import ssm.blog.entity.Picture;
import ssm.blog.service.PictureService;
import ssm.blog.util.DateJsonValueProcessor;
import ssm.blog.util.DateUtil;
import ssm.blog.util.ResponseUtil;
/**
 * @Description 相册Controller层
 * @author ZeusML
 *
 */
@Controller
@RequestMapping("/admin/album")
public class AlbumAdminController {

	@Resource
	private PictureService pictureService;

	@RequestMapping("/listPicture")
	public String listBlogType(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows, HttpServletResponse response)
			throws Exception {

		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("start", pageBean.getStart());
		map.put("pageSize", pageBean.getPageSize());
		List<Picture> pictureList = pictureService.listPicture(map);
		Long total = pictureService.getTotal();

		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(pictureList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}

	// 保存添加图片
	@RequestMapping("/save")
	public String save(@RequestParam("imageFile") MultipartFile imageFile,
					   Picture picture, 
					   HttpServletRequest request,
					   HttpServletResponse response) throws Exception {

		if (!imageFile.isEmpty()) { // 如果用户有传过照片，就更新
			String filePath = request.getSession().getServletContext().getRealPath("/"); // 获取服务器根路径
			String imageName = DateUtil.getCurrentDateStr() + "." + imageFile.getOriginalFilename().split("\\.")[1];
			imageFile.transferTo(new File(filePath + "static/album/" + imageName));
			picture.setImageName(imageName);
		}
		int resultTotal = pictureService.addPicture(picture);
		JSONObject result = new JSONObject();
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

	// 相片删除
	@RequestMapping("/delete")
	public String deleteBlog(@RequestParam(value = "ids", required = false) String ids, HttpServletResponse response)
			throws Exception {

		String[] idsStr = ids.split(",");
		JSONObject result = new JSONObject();
		for (int i = 0; i < idsStr.length; i++) {
			int id = Integer.parseInt(idsStr[i]);
			pictureService.deletePicture(id);
		}
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
}
